package com.hctg.forced2sleep.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.actor.myandroidframework.dialog.ViewBindingDialog;
import com.blankj.utilcode.util.SizeUtils;
import com.hctg.forced2sleep.bean.WeekBean;
import com.hctg.forced2sleep.databinding.DialogHourMinuteWeekSelectBinding;

import java.util.Calendar;

/**
 * description: 时分, 星期 选择
 * company    :
 *
 * @author : ldf
 * date       : 2025/1/6 on 14
 * @version 1.0
 */
public class HourMinuteWeekSelectDialog extends ViewBindingDialog<DialogHourMinuteWeekSelectBinding> {

    private String title = "请选择时间和星期";
    private final int hour, minute;
    private final WeekBean weekBean;
    private final OnConfirmClickListener listener;

    /**
     * @param title 标题
     * @param hour 时
     * @param minute 分
     */
    public HourMinuteWeekSelectDialog(@NonNull Context context, @Nullable String title,
                                      @IntRange(from = 0, to = 23) int hour,
                                      @IntRange(from = 0, to = 59) int minute,
                                      @Nullable WeekBean weekBean,
                                      @NonNull OnConfirmClickListener listener) {
        super(context);
        setWidthPercent(0.888888f, SizeUtils.dp2px(308f));
        if (!TextUtils.isEmpty(title)) this.title = title;
        this.hour = hour;
        this.minute = minute;
        if (weekBean == null) weekBean = new WeekBean();
        this.weekBean = weekBean;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.tvTitle.setText(title);
        //星期
        viewBinding.tvSunday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvMonday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvTuesday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvWednesday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvThursday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvFriday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        viewBinding.tvSaturday.setOnClickListener(v -> v.setSelected(!v.isSelected()));
        //
        viewBinding.tvCancel.setOnClickListener(v -> dismiss());
        viewBinding.tvConfirm.setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(viewBinding.dateTimePicker.getMillisecond());
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);

            weekBean.isSundaySelect = viewBinding.tvSunday.isSelected();
            weekBean.isMondaySelect = viewBinding.tvMonday.isSelected();
            weekBean.isTuesdaySelect = viewBinding.tvTuesday.isSelected();
            weekBean.isWednesdaySelect = viewBinding.tvWednesday.isSelected();
            weekBean.isThursdaySelect = viewBinding.tvThursday.isSelected();
            weekBean.isFridaySelect = viewBinding.tvFriday.isSelected();
            weekBean.isSaturdaySelect = viewBinding.tvSaturday.isSelected();
            listener.onConfirmClick(hour, minute, weekBean);
            dismiss();
        });

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        viewBinding.dateTimePicker.setDefaultMillisecond(calendar.getTimeInMillis());

        viewBinding.tvSunday.setSelected(weekBean.isSundaySelect);
        viewBinding.tvMonday.setSelected(weekBean.isMondaySelect);
        viewBinding.tvTuesday.setSelected(weekBean.isTuesdaySelect);
        viewBinding.tvWednesday.setSelected(weekBean.isWednesdaySelect);
        viewBinding.tvThursday.setSelected(weekBean.isThursdaySelect);
        viewBinding.tvFriday.setSelected(weekBean.isFridaySelect);
        viewBinding.tvSaturday.setSelected(weekBean.isSaturdaySelect);
    }

    public interface OnConfirmClickListener {
        void onConfirmClick(int hour, int minute, @NonNull WeekBean weekBean);
    }
}
